// @ts-nocheck
import { defineStore } from 'pinia';  
import router  from '@/router/index';

// 为什么用pinia存储动态路由？ 防止刷新后动态路由不见
// 不对，我下面接的就是子路由
export const useMenu = defineStore('menuInfor',{
  state: () => {
    return {
      menuData: [],  // 防止白屏
    }
  },
  actions: {
    async setRouter(arr){  
      const compilerMenu = (arr) => {
        if(!arr) return;
        this.menuData = arr;
        
        function loadComponent(url){  
          let Module = import.meta.glob("@/views/**/*.vue")  
          // console.log("Module信息:", Module); 
          const component = Module[`/src/views/${url}.vue`]  // 不是@
          // console.log("loadComponent信息:", component); 
          return component
        }
  
        arr.forEach((item) => {
          let componentPath = item.component
          let rts = {
            name: item.name,
            path: item.path,
            component: loadComponent(componentPath), 
          }
          // 有子路由时，router.addRoute('admin', { path: 'settings', component: AdminSettings })
          router.addRoute('menu', rts)
        })
      }
      compilerMenu(arr)
    },
    async reSetRouter(){
      this.setRouter(this.menuData)
    }
  },
  persist: true
})
